package com.atfcm.queue;

import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;

public class T_1296_划分数组为连续数字的集合 {

    /**
     * pageSize:2
     * pagNum:3
     *
     * [1,2,3,4,5,6]
     * @param nums
     * @param k
     * @return
     */
    public boolean isPossibleDivide(int[] nums, int k) {
        if( nums.length % k != 0 ){
            return false;
        }

        HashMap<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> queue = new PriorityQueue<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            queue.offer(nums[i]);
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }




        int pageNum = nums.length / k;
        int pageIndex = 0;
        while (pageIndex < pageNum) {
            int num = queue.poll();
            for (int i = 0; i < k-1; i++) {
                if(map.containsKey(num+1)){
                    Integer put = map.put(num + 1, map.getOrDefault(num+1, 0) - 1);
                    if(put <= 0){
                        map.remove(num+1);
                    }
                }
            }
        }




        return true;
    }
}
